import { createRouter, createWebHashHistory } from 'vue-router'

import Layout from '@/layout/index'

export const constantRoutes = [
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path(.*)',
        component: () => import('@/views/redirect/index.vue')
      }
    ]
  },
  {
    path: '/login',
    component: () => import('@/views/login/index.vue'),
    hidden: true
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        component: () => import('@/views/dashboard/index.vue'),
        name: 'Dashboard',
        meta: { title: 'Dashboard', icon: 'dashboard', affix: true }
      }
    ]
  },
  // 此写法解决动态路由页面刷新的 warning 警告
  {
    path: '/:pathMatch(.*)*',
    component: () => import('@/views/error-page/404.vue'),
    hidden: true
  }
]

// 用于和角色真正的路由进行过滤的路由
export const asyncRoutes = [
  {
    path: '/clipboard',
    component: Layout,
    name: 'Clipboard',
    children: [
      {
        path: 'index',
        component: () => import('@/views/clipboard/index.vue'),
        name: 'ClipboardIndex',
        meta: {
          title: 'Clipboard',
          icon: 'icon'
        }
      }
    ]
  },
  {
    path: '/excel',
    component: Layout,
    name: 'Excel',
    redirect: '/excel/export-excel',
    meta: {
      title: 'Excel',
      icon: 'icon'
    },
    children: [
      {
        path: 'export-excel',
        component: () => import('@/views/excel/export-excel.vue'),
        name: 'ExportExcel',
        meta: {
          title: 'Export Excel'
        }
      },
      {
        path: 'export-selected-excel',
        component: () => import('@/views/excel/select-excel.vue'),
        name: 'SelectExcel',
        meta: { title: 'Export Selected' }
      },
      {
        path: 'merge-header',
        component: () => import('@/views/excel/merge-header.vue'),
        name: 'MergeHeader',
        meta: { title: 'Merge Header' }
      },
      {
        path: 'upload-excel',
        component: () => import('@/views/excel/upload-excel.vue'),
        name: 'UploadExcel',
        meta: { title: 'Upload Excel' }
      }
    ]
  },
  {
    path: '/dom-to-image',
    name: 'DomToImage',
    component: Layout,
    children: [
      {
        path: 'index',
        component: () => import('@/views/dom-to-image/index.vue'),
        name: 'DomToImageIndex',
        meta: {
          title: 'DomToImage',
          icon: 'icon'
        }
      }
    ]
  },
  {
    path: '/permission',
    name: 'Permission',
    component: Layout,
    redirect: '/permission/role',
    children: [
      {
        path: 'role',
        component: () => import('@/views/permission/role'),
        name: 'PermissionRole',
        meta: { title: 'Permission', icon: 'icon' }
      }
    ]
  },
  {
    path: '/qrcode',
    name: 'Qrcode',
    component: Layout,
    redirect: '/qrcode/index',
    children: [
      {
        path: 'index',
        component: () => import('@/views/qrcode/index'),
        name: 'QrcodeIndex',
        meta: { title: 'Qrcode', icon: 'icon' }
      }
    ]
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes: constantRoutes
})

// 重置路由为静态路由
export const resetRouter = () => {
  router.getRoutes().forEach((route) => {
    const { name } = route
    if (name && asyncRoutes.find((item) => item.name === name)) {
      router.removeRoute(name)
    }
  })
}

export default router
